package cn.net.yugu.doraemon.datahub.config;

import cn.net.yugu.doraemon.datahub.core.DataHubConsumeHandler;
import cn.net.yugu.doraemon.datahub.core.DataHubTemplate;
import cn.net.yugu.doraemon.datahub.factory.DataHubClientFactory;
import com.aliyun.datahub.client.DatahubClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @description:
 * @author: wanghongli
 * @create: 2022-07-17 11:12:12
 */
@Configuration
@EnableConfigurationProperties(value = {DataHubProperties.class})
@ConditionalOnProperty(prefix = "aliyun.datahub", value = "enabled", havingValue = "true")
public class DataHubAutoConfiguration {
    @Bean
    public DatahubClient dataHubClient(DataHubProperties accountProperties) {
        return DataHubClientFactory.create(accountProperties);
    }

    @Bean
    @ConditionalOnProperty(prefix = "aliyun.datahub", value = "produce-enabled", havingValue = "true")
    public DataHubTemplate dataHubWrapper(DatahubClient datahubClient, DataHubProperties dataHubProperties) {
        return new DataHubTemplate(datahubClient, dataHubProperties);
    }

    @Bean
    @ConditionalOnProperty(prefix = "aliyun.datahub", value = "consume-enabled", havingValue = "true")
    public DataHubConsumeHandler dataHubConsumeHandler(DatahubClient datahubClient) {
        return new DataHubConsumeHandler(datahubClient);
    }
}
